package com.binarysoft.cmdhandler.common.httpclient;

import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

import com.binarysoft.service.common.httpclient.HttpClientService;
import com.binarysoft.service.common.httpclient.Parameter;

import common.framework.command.handler.AbstractCommandHandler;
import common.framework.command.server.Terminal;
import common.framework.dsb.annotation.DSBCmd;
import common.framework.dsb.client.DefaultServiceFactory;
import common.framework.dsb.client.ServiceFactory;

@DSBCmd("httpclient")
public class HttpClientHandler extends AbstractCommandHandler {

	@Override
	public void handle(String args, Terminal terminal) throws Exception {
		terminal.println("");
		StringTokenizer stier = new StringTokenizer(args);
		int count = stier.countTokens();
		if (count < 2) {
			terminal.println(" Usage:");
			terminal.println(" httpget  <url>                                   -- http get operation.");
			terminal.println(" httppost <url>                                   -- http post operation.");
			terminal.println("");
			return;
		}
		String cmd = stier.nextToken();
		String url = stier.nextToken();
		if ("httpget".equalsIgnoreCase(cmd)) {
			ServiceFactory serviceFactory = new DefaultServiceFactory();
			HttpClientService httpClientService = serviceFactory.lookup("DSB/HttpClientService");
			Collection<Parameter> params = new ArrayList<Parameter>();
			boolean flag = false;
			do {
				terminal.print("more parameter ? (y/n):");
				String opt = terminal.read();
				if ("n".equalsIgnoreCase(opt)) {
					flag = true;
				} else {
					terminal.print("input parameter name:");
					String name = terminal.read();
					terminal.print("input parameter value:");
					String value = terminal.read();
					Parameter p = new Parameter(name, value);
					params.add(p);
				}

			} while (!flag);
			Parameter[] arry = new Parameter[params.size()];
			String result = httpClientService.httpGet(url, params.toArray(arry));
			terminal.println("");
			terminal.println(result);

		} else if ("httppost".equalsIgnoreCase(cmd)) {
			ServiceFactory serviceFactory = new DefaultServiceFactory();
			HttpClientService httpClientService = serviceFactory.lookup("DSB/HttpClientService");
			Collection<Parameter> params = new ArrayList<Parameter>();
			boolean flag = false;
			do {
				terminal.print("more parameter ? (y/n):");
				String opt = terminal.read();
				if ("n".equalsIgnoreCase(opt)) {
					flag = true;
				} else {
					terminal.print("input parameter name:");
					String name = terminal.read();
					terminal.print("input parameter value:");
					String value = terminal.read();
					Parameter p = new Parameter(name, value);
					params.add(p);
				}

			} while (!flag);
			Parameter[] arry = new Parameter[params.size()];
			String result = httpClientService.httpPost(url, params.toArray(arry));
			terminal.println("");
			terminal.println(result);

		} else {
			terminal.println(" Usage:");
			terminal.println(" httpget  <url>                                   -- http get operation.");
			terminal.println(" httppost <url>                                   -- http post operation.");
		}
		terminal.println("");
	}
}